package com.zgl.modules.goods.controller;

import com.zgl.modules.goods.DTO.GoodsDTO;
import com.zgl.modules.goods.entity.GoodsEntity;
import com.zgl.modules.goods.service.Impl.GoodsServiceImpl;
import com.zgl.modules.order.DTO.OrderDTO;
import com.zgl.utils.Params;
import com.zgl.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Api(tags = "商品操作")
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsServiceImpl goodsService;
    @ResponseBody
    @PostMapping("/querGoodsAsc")
    @ApiOperation(value = "分页展示所有商品")
    public R  querGoodsAsc(@RequestBody Params<GoodsDTO> params){
        return goodsService.querGoodsAsc(params.getParams());
    }
    @ResponseBody
    @PostMapping("/saveOne")
    @ApiOperation(value = "新增一个商品")
    public R  saveOne(@RequestBody Params<GoodsDTO> params){

        GoodsEntity goodsEntity=new GoodsEntity();
        BeanUtils.copyProperties(params.getParams(), goodsEntity);
        return goodsService.savaOne(goodsEntity);
    }
    @ResponseBody
    @PostMapping("/modifyOne")
    @ApiOperation(value = "修改商品信息")
    public R modifyOne(@RequestBody Params<GoodsDTO> params){
        GoodsEntity goodsEntity=new GoodsEntity();
        BeanUtils.copyProperties(params.getParams(), goodsEntity);
        return goodsService.modifyOne(goodsEntity);
    }
    @ResponseBody
    @PostMapping("/removeOne")
    @ApiOperation(value = "删除一个商品")
    public R removeOne(@RequestBody GoodsDTO goodsDTO){return goodsService.removeOne(goodsDTO.getId());}
    @ResponseBody
    @PostMapping("/querGoodsByName")
    @ApiOperation(value = "根据商品名字查询商品")
    public R querGoodsByName(@RequestBody GoodsDTO goodsDTO){
        return  goodsService.querGoodsByName(goodsDTO);
    }
}
